<template>
  <div class="target-list-container">
    <!-- 搜索栏 -->
    <el-card class="search-container" shadow="never">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="合伙人名称">
          <el-input v-model="searchForm.name" placeholder="请输入合伙人名称" clearable @keyup.enter="handleSearch" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 表格 -->
    <el-card class="table-container" shadow="never">
      <template #header>
        <div class="card-header">
          <span>合伙人目标参数</span>
        </div>
      </template>

      <el-table v-loading="loading" :data="partnerTargetList" style="width: 100%" border>
        <el-table-column prop="id" label="ID" width="80" align="center" />
        <el-table-column prop="name" label="合伙人名称" min-width="150" />
        <el-table-column prop="incomeTarget" label="本月收入目标设置金额" min-width="180" align="center">
          <template #default="scope">
            {{ formatAmount(scope.row.incomeTarget) }} 万元
          </template>
        </el-table-column>
        <el-table-column prop="scaleTarget" label="月投放规模指标" min-width="180" align="center">
          <template #default="scope">
            {{ formatAmount(scope.row.scaleTarget) }} 万元
          </template>
        </el-table-column>
        <el-table-column label="目标进度" width="180">
          <template #default="scope">
            <el-progress :percentage="calculateProgress(scope.row)" :status="getProgressStatus(scope.row)" />
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination v-model:current-page="pagination.currentPage" v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { Search, Refresh } from '@element-plus/icons-vue';

defineOptions({
  name: "TargetLists"
});

// 搜索表单
const searchForm = reactive({
  name: '',
});

// 表格数据
const loading = ref(false);
const partnerTargetList = ref([]);

// 分页
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
});

// 加载数据
const loadData = () => {
  loading.value = true;

  // 这里应该调用实际的API
  setTimeout(() => {
    // 模拟数据
    partnerTargetList.value = [
      {
        id: 1,
        name: '张三公司',
        incomeTarget: 150,
        currentIncome: 120,
        scaleTarget: 500,
        currentScale: 350,
      },
      {
        id: 2,
        name: '李四金融服务',
        incomeTarget: 200,
        currentIncome: 80,
        scaleTarget: 800,
        currentScale: 450,
      },
      {
        id: 3,
        name: '王五投资',
        incomeTarget: 100,
        currentIncome: 95,
        scaleTarget: 300,
        currentScale: 290,
      }
    ];
    pagination.total = 3;
    loading.value = false;
  }, 500);
};

// 格式化金额
const formatAmount = (value) => {
  return value.toLocaleString();
};

// 计算进度
const calculateProgress = (row) => {
  if (!row.incomeTarget) return 0;
  const progress = (row.currentIncome / row.incomeTarget) * 100;
  return Math.min(100, Math.round(progress));
};

// 获取进度状态
const getProgressStatus = (row) => {
  const progress = calculateProgress(row);
  if (progress >= 100) return 'success';
  if (progress >= 80) return 'warning';
  return '';
};

// 搜索
const handleSearch = () => {
  pagination.currentPage = 1;
  loadData();
};

// 重置搜索
const resetSearch = () => {
  searchForm.name = '';
  handleSearch();
};

// 分页处理
const handleSizeChange = (val) => {
  pagination.pageSize = val;
  loadData();
};

const handleCurrentChange = (val) => {
  pagination.currentPage = val;
  loadData();
};

onMounted(() => {
  loadData();
});
</script>

<style scoped>
.search-container {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

:deep(.el-table th.el-table__cell) {
  background-color: #f5f7fa;
  color: var(--el-text-color-primary);
  font-weight: 500;
}
</style>